﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

public partial class revenues : System.Web.UI.Page
{
    CommonPage cm3 = new CommonPage();
    VLGDataContext _db8 = new VLGDataContext();
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["currentItem"] = "yoursp";
        if (!IsPostBack)
        {
            Page.Title = "Trang cá nhân | Lợi nhuận từ những dịch vụ";
            if (cm3.IsAuthenticated == false)
            {
                Response.Redirect(ResolveClientUrl("~/accessDenied.aspx"));
            }
            else
            {
                fillDulieuLenGrid();
            }
        }
    }

    void fillDulieuLenGrid()
    {

        int idUs = cm3.Customer.idAcc;
        //tong so tien trong tai khoan co the rut ngay
        var totalAvailble = (from a in _db8.VLGAccounts
                             where a.idAcc == idUs
                             select a.soTien).Single();
        ltrAvailable.Text = totalAvailble != null ? String.Format("{0:0.##}", totalAvailble) : "0";
        if (totalAvailble.HasValue && totalAvailble > 0)
        {
            ltrCheckOut.Text = "<a href=" + ResolveUrl("checkOut.aspx?id=" + idUs.ToString() + "&tk=" + totalAvailble.ToString()) + " >Click để thanh toán</a>";
        }
        else
            ltrCheckOut.Text = "Tài khoản không đủ để thanh toán";
        //tông số tiền đã rút qua paypal
        var totalWithrawn = (from c in _db8.YeuCauChuyenKhoans
                             where c.type == (int)EnumChuyenKhoanType.RutTien && c.idAcc == idUs && c.status == true&&c.isActive==true&&c.returnBalance!=true
                             select c).Sum(cs => cs.soTien);
        ltrAlready.Text = totalWithrawn != null ? String.Format("{0:0.##}", totalWithrawn) : "0";
        //tong so tien cac don hang da hoan thanh nhung admin chua chuyen khoan
        var totalWaiting = (from o in _db8.Orders
                            where o.Gig.idAcc == idUs && (o.CustomerConfirm == true)
                            && (o.DaThanhToan == false || o.DaThanhToan.HasValue == false)
                            && !(o.SellerCancel == true || o.CustomerCancel == true)
                            select o).Count() * 4;
        ltrWaiting.Text = String.Format("{0:0.##}", totalWaiting);
    //tong so tien da mua bang so tien kiem dc
        var totalRevenuePurchase = (from y in _db8.YeuCauChuyenKhoans
                                    where y.idAcc == idUs && !(y.Order.SellerCancel == true || y.Order.CustomerCancel == true)
                                    && (y.type == (int)EnumChuyenKhoanType.MuaTK)
                                    && (y.status == true)
                                    select y).Count() * 5;
        ltrRevenuePurchase.Text = String.Format("{0:0.##}", totalRevenuePurchase);
        //tong so tien cac don hang dang hoat dong tru cac don hang chua active, cancel, da thanh toan
        var totalFeature = (from p in _db8.Orders
                            where p.CustomerConfirm!=true&& p.SellerActive == true && p.CustomerActive == true && p.DaThanhToan != true && !(p.CustomerCancel == true || p.SellerCancel == true)
                            select p).Count() * 4;
        ltrFeature.Text = String.Format("{0:0.##}", totalFeature);


        var gigCanhan = from gg2 in _db8.YeuCauChuyenKhoans
                        where (gg2.idAcc == idUs) && (gg2.type == (int)EnumChuyenKhoanType.ThanhToanDH)
                        orderby gg2.ngay descending
                        select gg2;
        AspNetPager1.RecordCount = gigCanhan.Count();
        GridView1.DataSource = gigCanhan.Skip(AspNetPager1.PageSize * (AspNetPager1.CurrentPageIndex - 1)).Take(AspNetPager1.PageSize);
        GridView1.DataBind();
    }
    protected void AspNetPager1_PageChanged(object src, EventArgs e)
    {
        fillDulieuLenGrid();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {

    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {

    }
    protected void btnTiep2_Click(object sender, EventArgs e)
    {
        string noidung = TextBox1.Text.ToString().Trim();
        Response.Redirect(ResolveClientUrl("~/qlcongviec.aspx?op=add&to=") + noidung);
    }
}
